<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\Ad\AdPositionRequest;
use App\Http\Requests\Admin\Ad\UpdateAdPositionRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Ad;

use App\Models\AdPosition;

use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use App\Commands\OperationLogCommand;

class AdPositionController extends Controller
{

    public function ad_position_gate()
    {
        if (!Gate::allows('ad','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $this->ad_position_gate();

        $search = array('position_name'=>'');
        $adpos = AdPosition::paginate(10);
        if (!empty($request->get('position_name'))){
            $adpos = AdPosition::where('position_name', 'like', "%".trim($request->get('position_name'))."%")->paginate(10);
            $search['position_name'] = $request->position_name;
        }

        return view('admin.ad_position.index', compact('adpos','search'));

    }

    public function show($id)
    {

        $this->ad_position_gate();

        return Redirect('admin/ad/'.$id);

    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(AdPosition $adpos)
    {

        $this->ad_position_gate();

        return view('admin.ad_position.create',compact(['adpos']));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdPositionRequest $storeRequest,AdPosition $adpos)
    {
        $this->ad_position_gate();

        $adpos = $adpos->create($storeRequest->all());
        $this->dispatch(
            new OperationLogCommand('添加广告位','AdPosition','pos_id:'.$adpos->pos_id)
        );

        return Redirect('admin/ad_position');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($pos_id)
    {
        $this->ad_position_gate();

        //
        $ads = AdPosition::find($pos_id);
        return view('admin.ad_position.edit',compact('ads'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($pos_id,UpdateAdPositionRequest $updateRequest)
    {

        $this->ad_position_gate();

        $ads = AdPosition::find($pos_id);

        $ads->update($updateRequest->all());
        $this->dispatch(
            new OperationLogCommand('修改广告位','AdPosition','pos_id:'.$pos_id)
        );

        return Redirect('admin/ad_position');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($pos_id)
    {

        $this->ad_position_gate();

        $ads = AdPosition::find($pos_id);

        $ads->delete();

        $ads = Ad::where('pos_id', '=', $pos_id);

        $ads->delete();
        $this->dispatch(
            new OperationLogCommand('删除广告位','AdPosition','pos_id:'.$pos_id)
        );

        return Redirect('admin/ad_position');
    }
}
